package com.usian.controller;

import com.usian.pojo.Dept;
import com.usian.pojo.QueryVo;
import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("{/}")
    public String page(@PathVariable String page){
        return  page;
    }
    @RequestMapping("/toadd")
    public String toadd(Model model){
        List<Dept> list=userService.findDept();
        model.addAttribute("list" ,list);
        return "add";
    }
    @RequestMapping("/add")
    public String add(User user){
        userService.add(user);
        return "redirect:/user/ulist";
    }
    @RequestMapping("/ulist")
    public String findAll(Model model, QueryVo queryVo){
        List<QueryVo> list=userService.findAll(queryVo);
        model.addAttribute("list",list);
        return "ulist";
    }
    @RequestMapping("/delete")
    public String delete(Integer id){
        userService.delete(id);
        return "redirect:/user/ulist";
    }
    @RequestMapping("/findid")
    public String findid(Integer id,Model model) {
        User user=userService.findid(id);
        model.addAttribute("user",user);
        List<Dept> list=userService.findDept();
        model.addAttribute("list" ,list);
       return "edit";
    }
    @RequestMapping("/edit")
    public String edit(User user){
        userService.edit(user);
        return "redirect:/user/ulist";
    }
}
